﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Mortgage.Parallel;
using MortgageMonitor.DDL;
using Mortgage.DAL;
using Mortgage.DDL;
using MortgageMonitor.Forms;
using MortgageMonitor.Report;
using MortgageMonitor.Report.DDL;
using LoanMonitor.Business;
using TCKQKL.FrontEnd.DataModel;
using StockTrader.BusinessService.DataContract.Mortgate;
using System.IO;

namespace LoanMonitor.Utils
{
    public partial class frmTaskManager : Form
    {
        private List<AccountInfoUI> list = new List<AccountInfoUI>();
        public static decimal TotalLoan = 0;
        private int AccountCount;

        public frmTaskManager()
        {
            InitializeComponent();
        }
        private void taskManager_ReportWorkerProgress(object sender, MultithreadingWorker.ReportWorkerProgressEventArgs e)
        {
            // Find the matching item.
            foreach (ListViewItem item in lstDetail.Items)
            {
                if (item.Text == e.TaskID.ToString())
                {
                    item.SubItems[2].Text = e.Progress.ToString() + "% completed";
                    break;
                }
            }
        }

        private void taskManager_WorkerCompleted(object sender, MultithreadingWorker.WorkerCompletedEventArgs e)
        {
            DALclient client = new DALclient();
            // Find the matching item.
            foreach (ListViewItem item in lstDetail.Items)
            {
                if (item.Text == e.ID.ToString())
                {
                    item.SubItems[2].Text = "Completed";

                    // Store the results for later use.
                    // Could use a custom ListView here, but the
                    // Tag is a messier shortcut.
                    item.Tag = e.AccInfo;
                    list.Add(e.AccInfo);
                    AccountCount--;
                    break;
                }
            }

        }

        private void frmTaskManager_Load(object sender, EventArgs e)
        {
            btnControl.Enabled = true;
            taskManager.StartAllocatingWork(this);
        }

        private void btnControl_Click(object sender, EventArgs e)
        {
            DALclient client = new DALclient();
            List<string> str = new List<string>();
            //string[] str = { "059C002238", "059C000438" };

            List<AccountUI> customers = client.getCustomers();
            //customers.Add(new AccountUI(string.Empty, string.Empty, string.Empty, "011677432", DateTime.Now, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty));//client.getCustomers();
            //            customers.Add(new AccountUI(string.Empty, string.Empty, string.Empty, "012828385", DateTime.Now, string.Empty, string.Empty, string.Empty, string.Empty,string.Empty,string.Empty)) ;//client.getCustomers();
            foreach (AccountUI cus in customers)
                str.AddRange(client.getCustomerAccounts(cus.IDcardNo));

            //string[] str = { "059C002228", "059C000438" };
            btnControl.Enabled = false;

            GenerateAccountInfo userFsTask;


            //AccountCount = str.Length;
            foreach (string acc in str)
            {
                userFsTask = new GenerateAccountInfo(acc, false, false, false);
                ListViewItem item = new ListViewItem(userFsTask.ID.ToString());
                item.SubItems.Add(acc);
                item.SubItems.Add("Queued");
                lstDetail.Items.Add(item);

                taskManager.EnqueueTask(userFsTask);
            }

        }

        private void lstDetail_DoubleClick(object sender, EventArgs e)
        {
            // Check for item click.
            if (lstDetail.SelectedItems[0] != null)
            {
                //Check item is completed.
                if (lstDetail.SelectedItems[0] != null)
                {
                    MessageBox.Show(((CustomerFS)lstDetail.SelectedItems[0].Tag).AccountNo);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            try
            {
                DALclient client = new DALclient();
                List<AccountUI> customers = client.getCustomers();
                
                List<CustomerSumaryReport> data = new List<CustomerSumaryReport>();
                List<string> ActiveAccs = new List<string>();

                foreach (AccountUI cus in customers)
                {
                    List<string> Accs = client.getCustomerAccounts(cus.IDcardNo);
                    foreach (string acc in Accs)
                    {
                        AccountData account = client.getCustomerInfo(acc);
                        foreach (AccountInfoUI item in list)
                            TotalLoan += client.GetAccLoanByAccountNo(item.Identity.AccountNo);
                    }
                }

                foreach (AccountUI cus in customers)
                {
                    List<string> Accs = client.getCustomerAccounts(cus.IDcardNo);
                    decimal CustomerLoan = 0;
                    foreach (string acc in Accs)
                    {
                        WriteLog(acc);
                        //AccountData account = client.getCustomerInfo(acc);
                        foreach (AccountInfoUI item in list)
                            if (acc.Equals(item.Identity.AccountNo))
                            {
                                CustomerLoan += client.GetAccLoanByAccountNo(item.Identity.AccountNo);
                                WriteLog(acc + CustomerLoan.ToString());
                            }
                    }
                    WriteLog(CustomerLoan.ToString());
                    WriteLog("\n");
                    if (CustomerLoan > 0)
                    {
                        foreach (string acc in Accs)
                        {
                            foreach (AccountInfoUI item in list)
                            {
                                decimal AccLoan = client.GetAccLoanByAccountNo(acc);
                                if (item.Identity.AccountNo.Equals(acc))
                                    data.Add(new CustomerSumaryReport(cus.Name, item.Identity.Name,
                                            item.Identity.AccountNo,
                                            item.CashHolding.LoanLimit,
                                            item.CashHolding.StockValue,
                                            item.CashHolding.Cash,
                                            AccLoan,
                                            TotalLoan.Equals(0) ? 0 : AccLoan * 100 / TotalLoan,
                                            item.CashHolding.TotalValue));
                            }
                        }
                    }
                }


                if (data.Count > 0)
                {
                    rptLoanSummary rptAcc = new rptLoanSummary();
                    rptAcc.Load(Application.StartupPath + "\\Report\\rptLoanSummary.rpt");
                    rptAcc.SetDataSource(data);

                    frmReport report = new frmReport(rptAcc);
                    report.Show();
                }
                else
                    MessageBox.Show("Chẳng có gì để in cả!?", "Oops!");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Oops!");
                return;
            }
        }
        private void WriteLog(string logMsg)
        {
            using (StreamWriter wr = File.AppendText("HNX_version2.txt"))
            {
                lock (wr)
                {
                    wr.Write(DateTime.Now + ": ");
                    wr.WriteLine(logMsg);
                    wr.Flush();
                    wr.Close();
                }
            }
        }

    }
}
